<?php

declare(strict_types=1);

namespace App\Command;

use App\Model\PositionPaper;
use App\Repositories\PointPositionCategoryRepository;
use App\Repositories\PositionPaperRepository;
use Hyperf\Command\Command as HyperfCommand;
use Hyperf\Command\Annotation\Command;
use Psr\Container\ContainerInterface;

/**
 * @Command
 */
class UpdateCategoryScore extends HyperfCommand
{
    /**
     * @var ContainerInterface
     */
    protected $container;

    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;

        parent::__construct('update:score');
    }

    public function configure()
    {
        parent::configure();
        $this->setDescription('更新分类满分');
    }

    public function handle()
    {
        $this->line('update start', 'info');
        $categories = PointPositionCategoryRepository::getInstance()->all('*');
        foreach ($categories as $cat){
            $sum_score = PositionPaper::query()->where('category_id',$cat->id)->sum('score');
            if($sum_score==null){
                $sum_score=0;
            }
            $cat->full_score = $sum_score;
            $cat->save();
        }
        $this->line('update end','info');
    }
}
